/*
 * SocketServerTest.cpp
 */

#include "SocketServerTest.h"
#include "../../log/Log.h"
#include "../../sockets/ClientSocket.h"
#include "../../sockets/ByteBuffer.h"

SocketServerTest::SocketServerTest(bool detailedMode) : ss("4000"){
	ready = false;
	this->detailedMode = detailedMode;
}

SocketServerTest::~SocketServerTest() {
}

void SocketServerTest::run() {
	ClientSocket* cs = NULL;
	try {
		ss.doBind();
		ss.doListen();

		ready = true;
		cs = ss.doAccept();
		if (detailedMode) Log::getInstance()->info("[SERVER] Espero msg");

		// Recibo un msg del cliente
		char buffer[512];
		int reciv = cs->doReceive(buffer, 512);
		if (detailedMode) Log::getInstance()->info("[SERVER] Cadena recibida: " + std::string(buffer, reciv) );

		// Envio un msg al cliente
		ByteBuffer bf;
		bf.put("la recibi bien :D");
		cs->doSendComplete(&bf);
		if (detailedMode) Log::getInstance()->info("[SERVER] Envie cadena");

		cs->doClose();
	} catch (...) {
		Log::getInstance()->error("error en ServerTest..");
	}
	ready = true;
	if (cs != NULL)
		delete cs;
}

bool SocketServerTest::isReady() const {
	return ready;
}
